import store from '@/store';
import { HomeOutlined, PictureOutlined, FileTextOutlined, SettingOutlined, UserOutlined, SafetyCertificateOutlined, TeamOutlined, DashboardOutlined, InfoCircleOutlined, PictureOutlined as BannerOutlined, CrownOutlined, CalendarOutlined, FileImageOutlined, AppstoreOutlined, TagsOutlined, CommentOutlined, ThunderboltOutlined, UserSwitchOutlined, UsergroupAddOutlined, SafetyOutlined, TeamOutlined as RoleOutlined } from "@ant-design/icons-vue";
import iconImg from "@/assets/logogif.gif";
import axios from "axios";
import { useRouter } from 'vue-router';


console.log("Config")

export default {
    //#region Layout Config

    /* Name dislpayed in title bar */
    SystemTitle: "CapCap 管理系统",
    /* Image displayed in title bar, can be null or URL */
    SystemLogo: iconImg,
    /* Field name in userInfo indicating user's nickname of username, can be null */
    UserNameField: "userNick",
    /* Menu and sub menu items */
    MenuTree: [
        {
            key: "Home",
            title: "仪表盘",
            icon: HomeOutlined,
            subMenu: [
                {
                    key: "Dashboard",
                    title: "数据统计",
                    target: "dashboard",
                    icon: DashboardOutlined
                },
                // {
                //     key: "Home",
                //     title: "首页",
                //     target: "home",
                //     icon: HomeOutlined
                // },
                // {
                //     key: "About",
                //     title: "关于",
                //     target: "about",
                //     icon: InfoCircleOutlined
                // }
            ]
        },
        {
            key: "Homepage",
            title: "首页管理",
            icon: PictureOutlined,
            subMenu: [
                {
                    key: "BannerList",
                    title: "轮播图管理",
                    target: "banner-list",
                    icon: BannerOutlined
                },
                {
                    key: "RecommendList",
                    title: "推荐作品管理",
                    target: "recommend-list",
                    icon: CrownOutlined
                },
                {
                    key: "UpcomingList",
                    title: "活动预告管理",
                    target: "upcoming-list",
                    icon: CalendarOutlined
                }
            ]
        },
        {
            key: "Content",
            title: "内容管理",
            icon: FileTextOutlined,
            subMenu: [
                {
                    key: "ArtworkList",
                    title: "作品管理",
                    target: "artwork-list",
                    icon: FileImageOutlined
                },
                {
                    key: "CategoryList",
                    title: "分类管理",
                    target: "category-list",
                    icon: AppstoreOutlined
                },
                {
                    key: "TagList",
                    title: "标签管理",
                    target: "tag-list",
                    icon: TagsOutlined
                },
                {
                    key: "CommentList",
                    title: "评论管理",
                    target: "comment-list",
                    icon: CommentOutlined
                },
                {
                    key: "ActivityList",
                    title: "活动管理",
                    target: "activity-list",
                    icon: ThunderboltOutlined
                }
            ]
        },
        {
            key: "System",
            title: "系统管理",
            icon: SettingOutlined,
            subMenu: [
                {
                    key: "AdminList",
                    title: "管理员管理",
                    target: "admin-list",
                    icon: UserSwitchOutlined
                },
                {
                    key: "UserList",
                    title: "用户管理",
                    target: "user-list",
                    icon: UsergroupAddOutlined
                },
                {
                    key: "PermissionList",
                    title: "权限管理",
                    target: "permission-list",
                    icon: SafetyOutlined
                },
                {
                    key: "RoleList",
                    title: "角色管理",
                    target: "role-list",
                    icon: RoleOutlined
                }
            ]
        }
    ],
    /* Action to be performed when logout */
    LogoutAction: () => {
        localStorage.removeItem('token');
        store.dispatch({ type: "logout" });
        router.push('/login');
    },

    //#endregion

    //#region Router Config

    /* Route records */
    routes: [
        {
            path: '/login',
            name: 'login',
            meta: {
                requireAuth: false,
                title: '登录',
                layout: 'blank'
            },
            component: () => import('@/views/LoginView.vue')
        },
        {
            path: '/',
            name: 'dashboard',
            meta: {
                requireAuth: true,
                title: '数据统计',
                layout: 'pc',
                menuKey: "Home",
                subMenuKey: "Dashboard"
            },
            component: () => import('@/views/homepage/Dashboard.vue')
        },
        {
            path: '/home',
            name: 'home',
            meta: {
                requireAuth: true,
                title: 'HOME',
                layout: 'pc',
                menuKey: "Home",
                subMenuKey: "Home"
            },
            component: () => import('@/views/HomeView.vue')
        },
        {
            path: '/about',
            name: 'about',
            meta: {
                requireAuth: true,
                title: 'ABOUT',
                layout: 'pc',
                menuKey: "Home",
                subMenuKey: "About"
            },
            component: () => import('@/views/AboutView.vue')
        },
        {
            path: '/artwork/list',
            name: 'artwork-list',
            meta: {
                requireAuth: true,
                title: '作品管理',
                layout: 'pc',
                menuKey: "Content",
                subMenuKey: "ArtworkList",
                permission: "content:artwork:list"
            },
            component: () => import('@/views/content/ArtworkList.vue')
        },
        {
            path: '/category/list',
            name: 'category-list',
            meta: {
                requireAuth: true,
                title: '分类管理',
                layout: 'pc',
                menuKey: "Content",
                subMenuKey: "CategoryList",
                permission: "content:category:list"
            },
            component: () => import('@/views/content/CategoryList.vue')
        },
        {
            path: '/comment/list',
            name: 'comment-list',
            meta: {
                requireAuth: true,
                title: '评论管理',
                layout: 'pc',
                menuKey: "Content",
                subMenuKey: "CommentList",
                permission: "content:comment:list"
            },
            component: () => import('@/views/content/CommentList.vue')
        },
        {
            path: '/tag/list',
            name: 'tag-list',
            meta: {
                requireAuth: true,
                title: '标签管理',
                layout: 'pc',
                menuKey: "Content",
                subMenuKey: "TagList",
                permission: "content:tag:list"
            },
            component: () => import('@/views/content/TagList.vue')
        },
        {
            path: '/activity/list',
            name: 'activity-list',
            meta: {
                requireAuth: true,
                title: '活动管理',
                layout: 'pc',
                menuKey: "Content",
                subMenuKey: "ActivityList",
                permission: "content:activity:list"
            },
            component: () => import('@/views/content/ActivityList.vue')
        },
        {
            path: '/admin/list',
            name: 'admin-list',
            meta: {
                requireAuth: true,
                title: '管理员管理',
                layout: 'pc',
                menuKey: "System",
                subMenuKey: "AdminList",
                permission: "system:admin:manage:list"
            },
            component: () => import('@/views/system/AdminList.vue')
        },
        {
            path: '/user/list',
            name: 'user-list',
            meta: {
                requireAuth: true,
                title: '用户管理',
                layout: 'pc',
                menuKey: "System",
                subMenuKey: "UserList",
                permission: "system:user:manage:list"
            },
            component: () => import('@/views/system/UserList.vue')
        },
        {
            path: '/permission/list',
            name: 'permission-list',
            meta: {
                requireAuth: true,
                title: '权限管理',
                layout: 'pc',
                menuKey: "System",
                subMenuKey: "PermissionList",
                permission: "system:permission:list"
            },
            component: () => import('@/views/system/PermissionList.vue')
        },
        {
            path: '/role/list',
            name: 'role-list',
            meta: {
                requireAuth: true,
                title: '角色管理',
                layout: 'pc',
                menuKey: "System",
                subMenuKey: "RoleList",
                permission: "system:role:list"
            },
            component: () => import('@/views/system/RoleList.vue')
        },
        {
            path: '/banner/list',
            name: 'banner-list',
            meta: {
                requireAuth: true,
                title: '轮播图管理',
                layout: 'pc',
                menuKey: "Homepage",
                subMenuKey: "BannerList",
                permission: "homepage:banner:list"
            },
            component: () => import('@/views/homepage/BannerList.vue')
        },
        {
            path: '/recommend/list',
            name: 'recommend-list',
            meta: {
                requireAuth: true,
                title: '推荐作品管理',
                layout: 'pc',
                menuKey: "Homepage",
                subMenuKey: "RecommendList",
                permission: "homepage:recommended:list"
            },
            component: () => import('@/views/homepage/RecommendList.vue')
        },
        {
            path: '/upcoming/list',
            name: 'upcoming-list',
            meta: {
                requireAuth: true,
                title: '活动预告管理',
                layout: 'pc',
                menuKey: "Homepage",
                subMenuKey: "UpcomingList",
                permission: "homepage:upcoming:list"
            },
            component: () => import('@/views/homepage/UpcomingList.vue')
        }
    ],
    /* Action to be performed when checking login status */
    sessionAction: () => {
        return new Promise((resolve, reject) => {
            const token = localStorage.getItem('token');
            
            if (!token) {
                return resolve({ success: false, login: false });
            }

            axios.get('/api/admin/check-session')
                .then(response => {
                    if (response.data.code !== 200) {
                        return resolve({
                            success: false,
                            login: false,
                            error: response.data.message
                        });
                    }
                    console.log(response.data);
                    // 获取用户权限
                    return axios.get('/api/admin/permissions')
                        .then(permissionResponse => {
                            const permissions = permissionResponse.data.code === 200 
                                ? permissionResponse.data.data || [] 
                                : [];
                            
                            console.log('获取到的用户权限:', permissions);
                            
                            // 存储登录信息和权限
                            store.dispatch({
                                type: 'login',
                                data: {
                                    userName: response.data.data.username,
                                    userNick: response.data.data.nickname,
                                    userAccount: response.data.data.username,
                                    role: response.data.data.roles || [],
                                    avatar: response.data.data.avatar,
                                    permissions: permissions
                                }
                            });
                            
                            // 在权限获取和存储完成后再返回登录成功
                            resolve({
                                success: true,
                                login: true,
                                accountInfo: response.data.data
                            });
                        })
                        .catch(error => {
                            console.error('获取权限失败:', error);
                            // 即使获取权限失败，也继续登录流程，但确保在存储完成后再返回
                            store.dispatch({
                                type: 'login',
                                data: {
                                    userName: response.data.data.username,
                                    userNick: response.data.data.nickname,
                                    userAccount: response.data.data.username,
                                    role: response.data.data.roles || [],
                                    avatar: response.data.data.avatar,
                                    permissions: []
                                }
                            });
                            
                            resolve({
                                success: true,
                                login: true,
                                accountInfo: response.data.data
                            });
                        });
                })
                .catch(error => {
                    console.error('会话检查失败:', error);
                    localStorage.removeItem('token');
                    const router = useRouter();
                    router.push('/login');
                    resolve({
                        success: false,
                        login: false,
                        error: error.message
                    });
                });
        });
    },
    /* Name of the page to redirect if login check failed for pages that requires auth */
    authFallback: "login",

    //#endregion
};
